//
//  XTVisitorView.swift
//  Weibo(Swift)
//
//  Created by 肖霆 on 16/5/11.
//  Copyright © 2016年 肖霆. All rights reserved.
//

import UIKit
import SnapKit
class XTVisitorView: UIView {
    //设置闭包
    
    var CallBackClosure:(()->())?
    //  MARK: -懒加载控件-
    
    //旋转图片
    private lazy var cycImgView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    
    //遮罩
    private lazy var maskImgView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    //中心图片
    private lazy var iconImgView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //文字
    private lazy var messageLabel:UILabel = {
        var label = UILabel()
        
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        label.textColor = UIColor.darkGrayColor()
        label.textAlignment = .Center
        label.font = UIFont.systemFontOfSize(14)
        label.numberOfLines = 0
        
        return label
    }()

    //注册按钮
    private lazy var registerBtn:UIButton = {
        let btn = UIButton()
//        btn.titleLabel?.text = "注册"
        btn.setTitle("注册", forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(14)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        
        btn.addTarget(self, action: "registerAction", forControlEvents: .TouchUpInside)
        
        return btn
    }()
    //登陆按钮
    lazy private var loginBtn:UIButton = {
        var btn:UIButton = UIButton()
        btn.setTitle("登陆", forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        btn.titleLabel?.font = UIFont.systemFontOfSize(14)
        
        btn.addTarget(self, action: "loginAction", forControlEvents: .TouchUpInside)
        
        return btn
    
    }()
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //设置该view的相关控件
    private func setupUI(){
    backgroundColor = UIColor(white: 237/255, alpha: 1)
        
        addSubview(cycImgView)
        addSubview(maskImgView)
        addSubview(iconImgView)
        addSubview(messageLabel)
        addSubview(registerBtn)
        addSubview(loginBtn)
        
        //自动布局(SnapKit)
        cycImgView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        maskImgView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(cycImgView)
        }
        iconImgView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(cycImgView)
        }
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(cycImgView.snp_bottom)
            make.centerX.equalTo(cycImgView)
            make.size.equalTo(CGSizeMake(224, 40))
        }
        registerBtn.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(messageLabel)
            make.top.equalTo(messageLabel.snp_bottom).offset(10)
            make.size.equalTo(CGSizeMake(100, 35))
        }
        loginBtn.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(messageLabel)
            make.size.equalTo(registerBtn)
            make.top.equalTo(messageLabel.snp_bottom).offset(10)
        }
    
    }
    
    
    
    //给圆圈设置动画
    private func cycViewAnimation(){
    let animation = CABasicAnimation(keyPath: "transform.rotation")
        
        //目的地
        animation.toValue = M_PI * 2
        
        //完成所需时间
        animation.duration = 20
        
        //重复次数
        animation.repeatCount = MAXFLOAT
        
        //防止动画被释放掉
        animation.removedOnCompletion = false //Completion完成
        cycImgView.layer.addAnimation(animation, forKey: nil)
        
    
    }
    
    //根据title和imgView是否有值修改参观者视图
    func setVisitorInfo(title:String?,imgName:String?){
        if let t = title,name = imgName{
            
            //此时进入的不是首页
            messageLabel.text = t
            iconImgView.image = UIImage(named: name)
            
            //隐藏旋转圈
            cycImgView.hidden = true
        }else{
            cycImgView.hidden = false
            cycViewAnimation()
        }
    
    }
    
    //  MARK: -点击事件-
    @objc private func registerAction(){
        CallBackClosure?()
    }

    @objc private func loginAction(){
        CallBackClosure?()
    }
}
